package com.finalab.website.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.finalab.website.constant.ResponseCode;
import com.finalab.website.model.Graduate;
import com.finalab.website.service.GraduateService;
import com.finalab.website.util.BeanValidatorUtil;
import com.finalab.website.util.ResponseUtil;

@Controller
@RequestMapping(value = "/graduate")
public class GraduateController {

	@Autowired
	GraduateService gService;
	
	@RequestMapping(value = "/getById")
    public void selectGraduate(int id, PrintWriter out){
		Map<String, Object> map = new HashMap<>(1);
		map.put("graduate", gService.selectById(id));
		ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, map);
	}
	
	@RequestMapping(value = "/all")
    public void selectProject(Model model, PrintWriter out){
        model.addAttribute("graduates", gService.selectAll());
    }
	
	@RequestMapping(value = "/add")
	public void insert(Graduate graduate, PrintWriter out){
        if(!BeanValidatorUtil.validate(graduate)){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        gService.insert(graduate);
        ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED);
    }
	
	@RequestMapping(value = "/delete")
    public void delete(int id, PrintWriter out){
        gService.delete(id);
        ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED);
    }
	
	@RequestMapping(value = "/update")
    public void update(Graduate graduate, PrintWriter out){
        if(!BeanValidatorUtil.validate(graduate)){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        gService.update(graduate);
        ResponseUtil.responseCode(out, ResponseCode.REQUEST_SUCCESSED);
    }
	
}
